package com.imy.lua.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.imy.common.base.BaseCustomViewGroup;
import com.imy.common.util.ALog;
import com.imy.common.util.UiUtil;
import com.imy.lua.R;

public class CropRectView extends BaseCustomViewGroup {
    public CropRectView(Context context) {
        super(context);
    }

    public CropRectView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CropRectView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    private float oriX;
    private float oriY;
    private ImageView scaleIv;
    private int oriWidth;
    private int oriHeight;
    private RelativeLayout cropRl;

    @Override
    protected void init(View inflate) {
        scaleIv = inflate.findViewById(R.id.scale_iv);
        cropRl = inflate.findViewById(R.id.crop_rl);
        getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                getViewTreeObserver().removeOnGlobalLayoutListener(this);
                measure(0, 0);
                oriWidth = getMeasuredWidth();
                oriHeight = getMeasuredHeight();
                int deviceWidth = UiUtil.getDeviceWidth(mContext);
                int deviceHeight = UiUtil.getDeviceHeight(mContext);
                ALog.i("oriWidth:" + oriWidth + " height:" + oriHeight
                        + "deviceWidth:" + deviceWidth);
                setX(deviceWidth / 2 - oriWidth / 2);
                setY(deviceHeight / 2 - oriHeight / 2);
                oriX = getX();
                oriY = getY();


            }
        });
        setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                tryMoveView(event);
                return true;
            }
        });
        scaleIv.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                tryScaleView(event);
                return true;
            }
        });
    }

    private float scaleDownX;
    private float scaleDownY;

    private void tryScaleView(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                scaleDownX = event.getRawX();
                scaleDownY = event.getRawY();
                break;
            case MotionEvent.ACTION_MOVE:
                float moveX = event.getRawX() - scaleDownX;
                float moveY = event.getRawY() - scaleDownY;
                float newWidth = oriWidth + moveX;
                float newHeight = oriHeight + moveY;
                ViewGroup.LayoutParams layoutParams = cropRl.getLayoutParams();
                layoutParams.height = (int) newHeight;
                layoutParams.width = (int) newWidth;
                cropRl.setLayoutParams(layoutParams);
                setLog((int) newWidth, (int) newHeight, getX(), getY());
                break;
            case MotionEvent.ACTION_UP:
                layoutParams = cropRl.getLayoutParams();
                oriWidth = layoutParams.width;
                oriHeight = layoutParams.height;

                break;
        }
    }

    private float downX;
    private float downY;




    private void tryMoveView(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                downX = event.getRawX();
                downY = event.getRawY();
                break;
            case MotionEvent.ACTION_MOVE:
                float moveX = event.getRawX() - downX + oriX;

                setX(moveX);

                float moveY = event.getRawY() - downY + oriY;
                setY(moveY);

                measure(0, 0);
                int measuredWidth = getMeasuredWidth();
                setLog(measuredWidth, getMeasuredHeight(),
                        moveX, moveY);
                break;
            case MotionEvent.ACTION_UP:

                checkOut();

                break;
        }
    }

    private void checkOut() {
        measure(0, 0);
        int measuredWidth = getMeasuredWidth();
        int measuredHeight = getMeasuredHeight();

        float x = getX();
        float y = getY();
        if (x < 0) { //越界检测
            x = 0;
        } else {
            int deviceWidth = UiUtil.getDeviceWidth(mContext);
            Log.i(TAG, "width:" + deviceWidth + " x:" + x + " measuredWidth:" + measuredWidth);
            if (x + measuredWidth >= deviceWidth) {
                x = deviceWidth - measuredWidth;
            }
        }
        oriX = x;
        setX(x);
        if (y < 0) {
            y = 0;
        } else {
            int deviceHeight = UiUtil.getDeviceHeight(mContext);
            if (y + measuredHeight >= deviceHeight) {
                y = deviceHeight - measuredHeight;
            }
        }
        oriY = y;
        setY(y);


        setLog(measuredWidth, getMeasuredHeight(),
                getX(), getY());
    }

    @Override
    protected int getLayoutID() {
        return R.layout.custom_view_crop_rect;
    }

    public void hide() {
        setVisibility(GONE);
    }


    public void show() {
        setVisibility(VISIBLE);
        measure(0, 0);
        setLog(getMeasuredWidth(), getMeasuredHeight(), oriX, oriY);
    }

    public void setLogTv(TextView logTv) {
        this.logTv = logTv;
    }

    private TextView logTv;

    private void setLog(int width, int height, float x, float y) {
        if (logTv!=null){
            int x1 = (int) x;
            int y1 = (int) y;
            String content = "width:" + width + ", height:" + height + "\n"
                    + "x:" + x1 + ", y:" + y1;
            logTv.setText(content);
            logTv.setVisibility(VISIBLE);
        }
    }
}
